package com.bwie.commodity.controller;

import java.util.List;
import java.util.concurrent.*;
import javax.servlet.http.HttpServletResponse;

import com.bwie.commodity.domain.dto.CommodityBrandDTO;
import com.bwie.system.api.RemoteFileService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bwie.common.log.annotation.Log;
import com.bwie.common.log.enums.BusinessType;
import com.bwie.common.security.annotation.RequiresPermissions;
import com.bwie.commodity.domain.CommodityBrand;
import com.bwie.commodity.service.ICommodityBrandService;
import com.bwie.common.core.web.controller.BaseController;
import com.bwie.common.core.web.domain.AjaxResult;
import com.bwie.common.core.utils.poi.ExcelUtil;
import com.bwie.common.core.web.page.TableDataInfo;

/**
 * 品牌管理Controller
 *
 * @author ruoyi
 * @date 2024-11-13
 */
@RestController
@RequestMapping("/brand")
@Log4j2
public class CommodityBrandController extends BaseController
{
    @Autowired
    private ICommodityBrandService commodityBrandService;

    /**
     * 查询品牌管理列表
     */
    @RequiresPermissions("commodity:brand:list")
    @GetMapping("/list")
    public TableDataInfo list(CommodityBrand commodityBrand)
    {
        startPage();
        List<CommodityBrand> list = commodityBrandService.selectCommodityBrandList(commodityBrand);
        return getDataTable(list);
    }

    /**
     * 导出品牌管理列表
     */
    @RequiresPermissions("commodity:brand:export")
    @Log(title = "品牌管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommodityBrand commodityBrand)
    {
        List<CommodityBrand> list = commodityBrandService.selectCommodityBrandList(commodityBrand);
        ExcelUtil<CommodityBrand> util = new ExcelUtil<CommodityBrand>(CommodityBrand.class);
        util.exportExcel((jakarta.servlet.http.HttpServletResponse) response, list, "品牌管理数据");
    }

    /**
     * 获取品牌管理详细信息
     */
    @RequiresPermissions("commodity:brand:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(commodityBrandService.selectCommodityBrandById(id));
    }

    /**
     * 新增品牌管理
     */
    @RequiresPermissions("commodity:brand:add")
    @Log(title = "品牌管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommodityBrandDTO commodityBrandDTO)
    {
        commodityBrandService.updateOrInsertCommodityBrand(commodityBrandDTO);
        return success();
    }

    /**
     * 修改品牌管理
     */
    @RequiresPermissions("commodity:brand:edit")
    @Log(title = "品牌管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommodityBrandDTO commodityBrandDTO)
    {
        commodityBrandService.updateOrInsertCommodityBrand(commodityBrandDTO);
        return success();
    }

    /**
     * 删除品牌管理
     */
    @RequiresPermissions("commodity:brand:remove")
    @Log(title = "品牌管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(commodityBrandService.deleteCommodityBrandByIds(ids));
    }

    @GetMapping("test")
    public void test(){
//        System.out.println("开始访问");
//        long l1 = System.currentTimeMillis();
//        Future<String> r1 = commodityBrandService.test1();
//        Future<String> r2 = commodityBrandService.test2();
//        Future<String> r3 = commodityBrandService.test3();
//        while(true) {//死循环，每隔2000ms执行一次，判断一下这三个异步调用的方法是否全都执行完了。
//            if(r1.isDone() && r2.isDone() && r3.isDone()) {//使用Future的isDone()方法返回该方法是否执行完成
//                //如果异步方法全部执行完，跳出循环
//                break;
//            }
//            try {
//                Thread.sleep(2000);//每隔2000毫秒判断一次
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        }
//        long l2 = System.currentTimeMillis();//跳出while循环时说明此时三个异步调用的方法都执行完成了，此时得到当前时间
//
//        String result = null;
//        try {
//            result = r1.get();
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        } catch (ExecutionException e) {
//            throw new RuntimeException(e);
//        }
//        System.out.println("结束访问,用时"+(l2-l1));
//        System.out.println("使用get方法获得的返回内容:"+result);
//        return "finished";

        System.out.println("开始访问");
        long l1 = System.currentTimeMillis();

        CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
            commodityBrandService.test1();
            return "result";
        });

        CompletableFuture<String> cf2 = CompletableFuture.supplyAsync(() -> {
            commodityBrandService.test2();
            return "result";
        });

        CompletableFuture<String> cf3 = CompletableFuture.supplyAsync(() -> {
            commodityBrandService.test3();
            return "result";
        });
        long l2 = System.currentTimeMillis();

        System.out.println("结束访问,用时"+(l2-l1));

        try {
            System.out.println("使用get方法获得的返回内容:"+cf3.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }

//        System.out.println("开始访问");
//        long l1 = System.currentTimeMillis();
//        Future<String> r1 = commodityBrandService.test1();
//        Future<String> r2 = commodityBrandService.test2();
//        Future<String> r3 = commodityBrandService.test3();
//        String result2 = null;
//        try {
//            result2 = r2.get(50, TimeUnit.MILLISECONDS);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        } catch (ExecutionException e) {
//            throw new RuntimeException(e);
//        } catch (TimeoutException e) {
//            throw new RuntimeException(e);
//        }
//        System.out.println("使用有参get()得到的返回值"+result2);
//        while(true) {//死循环，每隔2000ms执行一次，判断一下这三个异步调用的方法是否全都执行完了。
//            if(r1.isDone() && r2.isDone() && r3.isDone()) {//使用Future的isDone()方法返回该方法是否执行完成
//                //如果异步方法全部执行完，跳出循环
//                break;
//            }
//            try {
//                Thread.sleep(2000);//每隔2000毫秒判断一次
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        }
//        long l2 = System.currentTimeMillis();//跳出while循环时说明此时三个异步调用的方法都执行完成了，此时得到当前时间
//
//        String result = null;
//        try {
//            result = r1.get();
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        } catch (ExecutionException e) {
//            throw new RuntimeException(e);
//        }
//		/*
//		 * String result2 = r2.get(50, TimeUnit.MILLISECONDS);
//		   System.out.println("使用有参get()得到的返回值"+result2);
//		   最开始在实验的时候把这两句话放在了这里，就一直测试不出来超时异常，
//		   后来才发现把超时设置在while循环判断已经任务完成之后，那么超时设置当然就不起作用了，
//		   所以放在这里也就不会出现超时异常，应该放在while循环之前即在任务开始执行之后就对其执行时长进行超时设置才会对时间真正起到限制作用。
//		 */
//        System.out.println("结束访问,用时"+(l2-l1));
//        System.out.println("使用get方法获得的返回内容:"+result);
//
//        return "finished";
    }
}
